package com.msite.service

import com.alibaba.fastjson.JSONObject
import com.msite.framework.PageResult
import com.msite.mapper.SysPermMapper
import com.msite.mapper.SysRoleMapper
import com.msite.mapper.SysUserMapper
import com.msite.model.SysPerm
import com.msite.model.SysRole
import com.msite.model.SysUser
import com.msite.model.param.SysUserParam
import com.msite.util.PageHelperUtil
import org.springframework.stereotype.Component

import javax.annotation.Resource
/**
 * Created by tpeng on 2015/11/25.
 */
@Component
class SysUserService {

    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysPermMapper sysPermMapper;

    SysUser selectByPrimaryKey(Long uid){
        sysUserMapper.selectByPrimaryKey(uid);
    }

    int insertSelective(SysUser record){
        sysUserMapper.insertSelective(record)
    }

    int updateByPrimaryKeySelective(SysUser record){
        sysUserMapper.updateByPrimaryKeySelective(record)
    }

    /**
     * 根据账户名查询用户信息
     * @param username
     * @return
     */
    SysUser getByUsername(String username){
        sysUserMapper.getByUsername(username)
    }

    /**
     * 查询管理员数据 - 分页 by Map
     * @param page
     * @param pernum
     * @return
     */
    PageResult getSysUserVoListByPage(SysUserParam param){
        PageHelperUtil.startPageResult(param.page, param.pagesize){
            sysUserMapper.getSysUserVoListBy(param);
        }
    }

    /**
     * 根据用户UserId查询用户角色信息
     * @param userId
     * @return
     */
    List<SysRole> getByUserId(Long userId){
        sysRoleMapper.getByUserId(userId);
    }

    /**
     * 根据用户角色Ids查询权限信息
     * @param roleIds
     * @return
     */
    List<SysPerm> getByRoleIds(List<Long> roleIds){
        def plist = new ArrayList<SysPerm>();
        if(roleIds && roleIds.size() > 0){
            plist = sysPermMapper.getByRoleIds(roleIds)
        }
        plist
    }

    /**
     * 查询管理员角色列表Json数据
     * @return
     */
    String selectRoleListJsonData(){
        List<SysRole> rlist = sysRoleMapper.selectListAllBy()
        def list = rlist.inject([]){
            lst,v ->
                lst << ["id" : v.id, "text" : v.roleName ]
        }
        JSONObject.toJSONString(list)
    }
}
